package com.huiyujinrong.qiangdanbao.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import static android.content.ContentValues.TAG;


public class DataUtils {

    private static DataUtils utils;

    /**
     * 单例模式
     *
     * @return
     */
    public static DataUtils getInstance() {
        if (utils == null) {
            synchronized (DataUtils.class) {
                if (utils == null) {
                    utils = new DataUtils();
                }
            }
        }
        return utils;
    }


    public static boolean getCheckTel(String mobiles) {
        String telRegex = "[1][3578]\\d{9}";//"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        if (TextUtils.isEmpty(mobiles) && mobiles.length() < 11) return false;
        else return mobiles.matches(telRegex);
    }


    /**
     * 根据 时间戳  转换为 时间
     *
     * @param time 格式 yyyy年MM月dd日  HH时mm分ss秒
     * @return
     */
    public static String getMillisecond(long time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy.MM.dd HH.mm.ss");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        String times = sdr.format(new Date(time));
        return times;

    }

    public static String getDate(long time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        String times = sdr.format(new Date(time));
        return times;

    }

    /**
     * 将时间字符串转为时间戳
     * <p>
     * <p>time格式为pattern</p>
     *
     * @param time    时间字符串
     * @param pattern 时间格式
     * @return 毫秒时间戳
     */

    public static long string2Millis(String time, String pattern) {
        try {

            return new SimpleDateFormat(pattern, Locale.getDefault()).parse(time).getTime();

        } catch (ParseException e) {

            e.printStackTrace();

        }

        return -1;

    }


    /**
     * 时间戳转换为 毫秒数
     *
     * @param time
     * @return
     */
    public static long getSecond(long time) {
        //获取系统时间戳
        long system = System.currentTimeMillis();
        //系统时间戳 - 获取时间戳 = 豪秒数
        long times = (system - time);
        return times;
    }

    private static final int seconds_of_1minute = 60;

    private static final int seconds_of_30minutes = 30 * 60;

    private static final int seconds_of_1hour = 60 * 60;

    private static final int seconds_of_1day = 24 * 60 * 60;

    private static final int seconds_of_15days = seconds_of_1day * 3;

    private static final int seconds_of_30days = seconds_of_1day * 30;

    private static final int seconds_of_6months = seconds_of_30days * 6;

    private static final int seconds_of_1year = seconds_of_30days * 12;

    /**
     * @return timtPoint距离现在经过的时间，分为
     * 刚刚，1-29分钟前，半小时前，1-23小时前，1-14天前，半个月前，1-5个月前，半年前，1-xxx年前
     */
    public static String getTimeElapse(Long createTime) {

        long nowTime = new Date().getTime() / 1000;

        Log.i(TAG, "getTimeElapse: " + nowTime);
        //createTime是发表文章的时间

        long oldTime = createTime;

        //elapsedTime是发表和现在的间隔时间

        long elapsedTime = nowTime - oldTime;

        if (elapsedTime < seconds_of_1minute) {

            return "刚刚";
        }
        if (elapsedTime < seconds_of_30minutes) {

            return elapsedTime / seconds_of_1minute + "分钟前";
        }
        if (elapsedTime < seconds_of_1hour) {

            return "半小时前";
        }
        if (elapsedTime < seconds_of_1day) {

            return elapsedTime / seconds_of_1hour + "小时前";
        }
        if (elapsedTime <= seconds_of_15days) {

            return elapsedTime / seconds_of_1day + "天前";
        }
        if (elapsedTime > seconds_of_15days) {
            return getDate(createTime * 1000);
        }
//        if (elapsedTime < seconds_of_30days) {
//
//            return "半个月前";
//        }
//        if (elapsedTime < seconds_of_6months) {
//
//            return elapsedTime / seconds_of_30days + "月前";
//        }
//        if (elapsedTime < seconds_of_1year) {
//
//            return "半年前";
//

        return "";
    }

    public static String getTimeAge(Long createTime) {

        long nowTime = new Date().getTime() / 1000;

        Log.i(TAG, "getTimeElapse: " + nowTime);
        //createTime是发表文章的时间

        long oldTime = createTime;

        //elapsedTime是发表和现在的间隔时间

        long elapsedTime = nowTime - oldTime;

        if (elapsedTime >= seconds_of_1year) {

            return elapsedTime / seconds_of_1year + "岁";
        } else {
            return "";
        }
    }

    public static String getStringData(String data) {
        if (data.contains(">")) {
            data = data.replace(">", "");
        }
        if (data != "" & data != null) {
            if (data.indexOf("-") == -1) {
                return data;
            } else {
                String result = data.substring(data.lastIndexOf("-") + 1, data.length());
                return result;
            }
        }
        return "";
    }

    public static String sHA1(Context context) {
        StringBuffer hexString;
        String result;
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            result = hexString.toString();
            return result.substring(0, result.length() - 1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
